// 简单工厂模式
package main

import "fmt"

// -----抽象层-----

type Fruit interface {
	Show()
}

// -----实现层-----

type Apple struct{}

func (apple *Apple) Show() {
	fmt.Println("This is apple")
}

type Banana struct{}

func (banana *Banana) Show() {
	fmt.Println("This is banana")
}

// =====工厂模块=====

type Factory struct{}

func (factory *Factory) CreateFruit(kind string) Fruit {
	var fruit Fruit

	if kind == "apple" {
		// apple的构造初始化业务
		fruit = new(Apple) // 满足多态条件的赋值，父类指针指向子类对象
	} else if kind == "banana" {
		// banana的构造初始化业务
		fruit = new(Banana) // 满足多态条件的赋值，父类指针指向子类对象
	}

	return fruit
}

// -----业务逻辑层-----
func main() {
	factory := new(Factory)
	apple := factory.CreateFruit("apple")
	apple.Show()
}
